<?php

namespace App\Jobs;

use App\Constant\QueueKey;
use App\Constant\ThirdMoneyCode;
use App\Facades\MoneyPlatformRequestFacade;
use App\Models\RepayRecordCommon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class RepayMoneyNotifyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;

    protected $commonInfo;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($model)
    {
        $this->onQueue(QueueKey::QUEUE_REPAY_MONEY_NOTIFY_RESULT);
        $this->commonInfo = $model;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $model = $this->commonInfo;
        if (RepayRecordCommon::MONEY_NOTIFY_STATUS_TWO == $model->money_notify_status) {
            $result = MoneyPlatformRequestFacade::getMoneyRepayResult([
                'third_repay_id' => $model->money_notify_third_record_id,
                'trade_no' => $model->money_notify_trade_no,
            ]);
            $data = $result['data'];
            if (ThirdMoneyCode::RESULT_STATUS_SUCCESS == $data['resultStatus']) {
                $model->money_notify_status = RepayRecordCommon::MONEY_NOTIFY_STATUS_FOUR;
                $model->save();

            } else if (ThirdMoneyCode::RESULT_STATUS_FAILURE == $data['resultStatus']) {
                //失败
                $model->money_notify_status = RepayRecordCommon::MONEY_NOTIFY_STATUS_THREE;
                $model->save();
            }
            Log::debug('repay_money_notify_result', ['message' => json_encode($data,JSON_UNESCAPED_UNICODE)]);
            $this->delete();
        }
        return ;
    }
}
